@Midway.MidwayModule({
    code: "Compare",
})
class Compare extends CommonClass {
    constructor() {
        super();
        this.metaInfo.name = "Compare";
        this.metaInfo.type = ConstVariable.PresenterType.LAUNCHER;
        this.adaptedModel = [];
        this.componentSetting = {};
    }

    $launch() {
        this.getCompareContent(this.Model).then(() => {
            this.adaptedOriginData(this.Model);
            this.configComponents();
            this.adaptedModel.push({
                name: "Components",
                type: "json",
                text: JSON.stringify(this.componentSetting)
            });
        }).then(() => {
            return this.requireComponent('ComponentLoader', this.adaptedModel, $(this.View), {
                left: 0,
                top: 0,
                width: 0,
                height: 0
            });
        })
    }

    getSkin() {
        if(this.Environment.isWeb) {
            return "web_default";
        } else if(this.Environment.isApp) {
            return "app_default";
        }
    }

    update(view, model) {
        this.adaptedModel = [];
        this._model = model;
        this.getCompareContent(this.Model);
        this.adaptedOriginData(model);
        let adaptedData = {};
        this.adaptedModel.forEach(item => {
            if (item.name === 'CompareRemake') {
                const data = JSON.parse(item.value);
                let tModel = {};
                data.forEach(prop => {
                    if(prop.type === 'string') {
                        tModel[prop.name] = prop.value;
                    } else if(prop.type === 'json') {
                        tModel[prop.name] = JSON.parse(prop.text);
                    }
                });
                adaptedData[item.name] = tModel;
            }

        });
        return {dispatch: true, adaptedModel: adaptedData};
    }

    adaptedOriginData(model) {
        var self = this;
        //添加题型所需要的数据
        this.adaptedModel.push({"name": "questionType", "type": "string", "value": "CompareRemake"});
        this.adaptedModel.push({
            name: "TimerController",
            type: "string",
            value: JSON.stringify([
                {
                    name: "timer_type",
                    type: "string",
                    value: model.timer_type
                }, {
                    name: "time_limit",
                    type: "string",
                    value: model.time_limit
                }, {
                    name: "isPauseOnSubmit",
                    type: "boolean",
                    value: "false"
                }
            ])
        });
        this.adaptedModel.push({
            name: "InteractionSubmitButton",
            type: "string",
            value: JSON.stringify([
                {
                    name: "can_modify",
                    type: "string",
                    value: true
                }, {
                    name: "linkQuestionType",
                    type: "string",
                    value: "compare"
                }, {
                    name: "linkQuestionId",
                    type: "string",
                    value: model['question_id']
                }, {
                    name: "view_type",
                    type: "string",
                    value: "new_em_button"
                }
            ])
        });
        this.adaptedModel.push({
            name: "InteractionTimer",
            type: "string",
            value: JSON.stringify([
                {
                    "name": "view_type",
                    "type": "string",
                    "value": "new_timer"
                }
            ])
        });
        this.adaptedModel.push({
            name: "CompareRemake",
            type: "string",
            value: JSON.stringify([
                {
                    "name": "questionId",
                    "type": "string",
                    "value": model.question_id
                }, {
                    name: "questionData",
                    type: "json",
                    text: JSON.stringify(self.compareData)
                }
            ])
        });

        this.adaptedModel.push({
            name: "ObjectiveInteractionLayout",
            type: "string",
            value: JSON.stringify([
                {
                    name: "questionType",
                    type: "string",
                    value: "Compare"
                }, {
                    name: "specialTemplate",
                    type: "string",
                    value: "false"
                }
            ])
        });
        this.adaptedModel.push({
            name: "AnswerFlowController",
            type: "string",
            value: JSON.stringify([
                {
                    name: "showResultOnSubmit",
                    type: "string",
                    value: "false"
                }
            ])
        });
    }

    configComponents() {
        //根据不同的运行场景定义需要加载的组件
        if (this.embedApplication('pptshell') || this.Environment.isWeb) {
            //如果是PPTShell或Web的场景
            this.componentSetting = {
                controllers: [{
                    presenter: "AnswerFlowController",
                    depends: [
                        {
                            property: "TimerService",
                            presenter: "TimerController"
                        }
                    ]
                }
                ],
                layout: "ObjectiveInteractionLayout",
                components: [
                    {
                        presenter: "InteractionTimer",
                        renderTo: "TopLeftPanel"
                    }, {
                        presenter: "InteractionSubmitButton",
                        renderTo: "BottomPanel"
                    }, {
                        presenter: "CompareRemake",
                        renderTo: "CenterPanel"
                    }
                ]
            };
        } else if (this.Environment.isStudent) {
            //如果是学生端Pad
            this.componentSetting = {
                controllers: [
                    {
                        presenter: "InteractionStateRecoverFilter"
                    }, {
                        presenter: 'I18nFilter'
                    }, {
                        presenter: "AnswerFlowController",
                        depends: [
                            {
                                property: "TimerService",
                                presenter: "TimerController"
                            }
                        ]
                    }, {
                        presenter: "AnswerProgressController"
                    }
                ],
                layout: "ObjectiveInteractionLayout",
                components: [
                    {
                        presenter: "InteractionTimer",
                        renderTo: "TopLeftPanel"
                    }, {
                        presenter: "InteractionSubmitButton",
                        renderTo: "TopRightPanel"
                    }, {
                        presenter: "CompareRemake",
                        renderTo: "CenterPanel"
                    }
                ]
            };
        }
    }

    getCompareContent(model) {
        var self = this;
        var deffer = $.Deferred();
        if (typeof model.question_url === 'string') { //qti-model
            var option = {
                'refPath': Midway.Settings.BasePath
            };
            QtiPlayerOldStyle.load(model.question_url, option, function (modelInfo) {
                var correct = [];
                $.each(modelInfo.correctAnswer, function (k, v) {
                    var response = {};
                    response.key = k;
                    response.baseType = v.baseType;
                    response.cardinality = v.cardinality;
                    response.identifier = v.identifier;
                    //helper.response.push(response);
                    correct.push(v.value);
                });
                //目前都是一道题模式，直接取数组的第一个
                var symbols = correct[0];

                var bodyObj = modelInfo.model;
                var body = [];
                for (var k = 0, lenk = bodyObj.length; k < lenk; k++) {
                    var itemHtml = bodyObj[k].prompt;
                    var entry = $(itemHtml).find('textEntry');
                    var matchs = [];
                    for (var i = 0, len = entry.length; i < len; i++) {
                        var match = {};
                        var temp = $(entry[i]).parent().html().split('<textentry></textentry>');
                        match.first = temp[0];
                        match.last = temp[1];
                        matchs.push(match);
                    }
                    body.push(matchs);
                }

                var items = body[0];

                var data = [];
                for (var si = 0, slen = symbols.length; si < slen; si++) {
                    data.push({"left": items[si].first, "symbol": symbols[si], "right": items[si].last});
                }
                self.compareData = {
                    "title": model.question_title,
                    "items": data
                };
                deffer.resolve();
            });
        } else {
            self.compareData = {
                title: model.question_title,
                items: model.question_url
            };
            deffer.resolve();
        }
        return deffer;
    }
}